
subjectList={'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19',
'20',
'21',
'22',
'23',
'24',
'25',
'26',
'27',
'28',
'29',
'30',
'31',
'32',
'33',
'34',
'35',
'36'}


for i = 1:length(subjectList)
clearvars -except subjectList i
subject=char(subjectList(i));

%% Part 1 - up to filtered epochs
%Pretend like we are running one participant

%load the data
EEG = pop_loadbv('/Users/jsprouse/Desktop/class data/', [subject '.vhdr']);
EEG.setname=subject;

%high pass filter at .1 (remove DC offset)
EEG  = pop_basicfilter( EEG,  1:32 , 'Cutoff',  0.1, 'Design', 'butter', 'Filter', 'highpass', 'Order',  2, 'RemoveDC', 'on');

%(interpolate bad channels)

%re-referencing 
EEG = pop_eegchanoperator( EEG, '/Users/jsprouse/Desktop/rereference.for.erplab.txt')

%channel locations
EEG=pop_chanedit(EEG, 'lookup','/Users/jsprouse/Documents/MATLAB/eeglab2020_0/plugins/dipfit/standard_BESA/standard-10-5-cap385.elp');

%add an event list
EEG  = pop_editeventlist( EEG , 'AlphanumericCleaning', 'on', 'BoundaryNumeric', { -99}, 'BoundaryString', { 'boundary' }, 'List', '/Users/jsprouse/Desktop/N400.eventlist.txt', 'SendEL2', 'EEG', 'UpdateEEG', 'off', 'Warning','off' );

%extract bin-based epochs
EEG = pop_epochbin( EEG , [-200.0  1000.0],  'pre');

%(save point)

%low pass filter (remove DC offset)
EEG  = pop_basicfilter( EEG,  1:32 , 'Cutoff',  30, 'Design', 'butter', 'Filter', 'lowpass', 'Order',  2, 'RemoveDC', 'on' );

%save point
EEG = pop_saveset( EEG, 'filename',[subject '.30hz.set'],'filepath','/Users/jsprouse/Desktop/binned 30hz');

%end loop
end

%% Part 2 - artifact detection
%Here are some things you might want to do in a script.
%(detrend)
%artifact detection
    %interpolate
    %threshold test
    %trend test

%Here is me trying to do them    
subjectList={'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19',
'20',
'21',
'22',
'23',
'24',
'25',
'26',
'27',
'28',
'29',
'30',
'31',
'32',
'33',
'34',
'35',
'36'}

%threshold list for detection
thresholdList=[65,
100,
110,
80,
120,
100,
75,
150,
120,
75,
120,
100,
75,
74,
80,
100,
100
100,
80,
100,
100,
100,
100,
140,
100,
100,
100,
100,
100,
100,
60,
100,
100,
75,
80,
80]


for i = 1:length(subjectList)
	%clear whatever happened in the last loop, keep the subjectList and the variable i
	clearvars -except subjectList thresholdList i
	
	%choose a subject from the list
	subject=char(subjectList(i));



%open the dataset
	EEG = pop_loadset([subject '.30hz.set'], '/Users/jsprouse/Desktop/binned 30hz/');

%interpolate bad electrodes (need to look up channel numbers)
if i==14
EEG = eeg_interp(EEG, 9);
EEG = eeg_interp(EEG, 15);
end

if i==16
EEG = eeg_interp(EEG, 9);
EEG = eeg_interp(EEG, 25); 
end

if i==19
EEG = eeg_interp(EEG, 4);
end

if i==22
EEG = eeg_interp(EEG, 16);
EEG = eeg_interp(EEG, 18);
EEG = eeg_interp(EEG, 17);
end

if i==30
EEG = eeg_interp(EEG, 9);
EEG = eeg_interp(EEG, 11);
end

if i==33
EEG = eeg_interp(EEG, 9);
EEG = eeg_interp(EEG, 25);
EEG = eeg_interp(EEG, 32);
EEG = eeg_interp(EEG, 26);
end

%detrend
EEG = lindetrend( EEG, 'all');

%threshold
%this is basically blinks.
%the 0 in the second to last argument is meaningful. Setting it to 1 makes the marks disappear after running the next one? (It is about superposing, but not sure why this happens) 
EEG = pop_eegthresh(EEG, 1, [1:32], -thresholdList(i), thresholdList(i), -.2, .999, 0, 0)

%linear
EEG = pop_rejtrend(EEG, 1, [1:32], 1199, 50, .3, 1, 0, 0)


%save the dataset
EEG = pop_syncroartifacts(EEG, 'Direction', 'eeglab2erplab');
EEG = pop_saveset( EEG, 'filename',[subject '.30hz.set'],'filepath','/Users/jsprouse/Desktop/no artifacts/');

end
    
    
    
%% Part 3 - create ERPs


%loop
subjectList={'1',
'2',
'3',
'4',
'5',
'6',
'7',
'8',
'9',
'10',
'11',
'12',
'13',
'14',
'15',
'16',
'17',
'18',
'19',
'20',
'21',
'22',
'23',
'24',
'25',
'26',
'27',
'28',
'29',
'30',
'31',
'32',
'33',
'34',
'35',
'36'}

for i = 1:length(subjectList)
	%clear whatever happened in the last loop, keep the subjectList and the variable i
	clearvars -except subjectList i
	
	%choose a subject from the list
	subject=char(subjectList(i));

%open the dataset
	EEG = pop_loadset([subject '.30hz.set'], '/Users/jsprouse/Desktop/no artifacts/');
	
	%calculate ERPs
ERP = pop_averager(EEG , 'Criterion', 'good', 'ExcludeBoundary', 'on', 'SEM', 'on' );

%make a new bin?
ERP = pop_binoperator( ERP, {'b3=b2-b1 label N400'})

%change the baseline?
ERP = pop_blcerp( ERP , 'Baseline', [300 500], 'Saveas', 'off' );

%export ERP for use in other language (R?)
ERP = pop_export2text( ERP, ['/Users/jsprouse/Desktop/exported ERPs/' subject '.txt'],  1:3, 'electrodes', 'on', 'precision',  4, 'time', 'on', 'timeunit',  0.001, 'transpose', 'on' );

%save the ERP
ERP = pop_savemyerp(ERP, 'erpname',...
 subject, 'filename', [subject '.erp'], 'filepath', '/Users/jsprouse/Desktop/ERPs', 'Warning', 'off');

end


%Calculate grand average ERP with a loop

%comment out participants we want to exclude
subjectList={'1',
'2',
'3',
'4',
'5',
'6',
'7',
%'8',
%'9',
'10',
'11',
%'12',
'13',
'14',
%'15',
'16',
'17',
%'18',
'19',
'20',
'21',
'22',
'23',
'24',
%'25',
'26',
'27',
'28',
'29',
'30',
'31',
%'32',
'33',
'34',
'35',
'36'}


%create overarching structure for all participant ERPs
ALLERP = buildERPstruct([]);

%loop
for i = 1:length(subjectList)
	%clear whatever happened in the last loop, keep the subjectList and the variable i
	clearvars -except subjectList i ALLERP
	
	%choose a subject from the list
	subject=char(subjectList(i));

ALLERP(i) = pop_loaderp( 'filename', [subject '.erp'], 'filepath', '/Users/jsprouse/Desktop/ERPs/' );

end

%create grandaveraged ERP

%remove ERP just in case it interferes
clear ERP

%create grandaverage
ERP = pop_gaverager( ALLERP , 'ERPindex', 1:length(subjectList), 'ExcludeNullBin', 'on', 'SEM', 'on' );

%save the ERP
ERP = pop_savemyerp(ERP, 'erpname', 'grand.average', 'filename', 'grand.average.erp', 'filepath', '/Users/jsprouse/Desktop/ERPs/', 'Warning', 'on');

%% Plot in Matlab?

%% Run statistics mass univariate toolbox

